package com.hoshiicloud.shxping.task;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.DateUtil;
import com.hoshiicloud.saleorder.dto.OrderDTO;
import com.hoshiicloud.saleorder.param.OrderMainIdsUpdateParam;
import com.hoshiicloud.shxping.feign.OrderFeignService;
import com.hoshiicloud.shxping.param.OrderQueryParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ygd
 * @title: verificationOrderTask
 * @projectName hoshiicloud
 * @description: 自动核销订单任务
 */
@Slf4j
@Component
public class VerificationOrderTask {

    @Autowired
    private OrderFeignService orderFeignService;

    /**
     * 30分钟执行一次
     */
    //@Scheduled(cron = "0 0/30 * * * ?")
    public void scheduled() {

        try {
            log.info("自动核销订单任务begin----", System.currentTimeMillis());

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            //查询待提货订单
            OrderQueryParam param = new OrderQueryParam();
            param.setPageNumber("1");
            param.setPageSize("20");
            param.setStatus(3);
            Response<Page<OrderDTO>> response = orderFeignService.listOrder(param);

            if (response != null && response.getData() != null) {
                Page<OrderDTO> orderDTOPage = response.getData();
                List<OrderDTO> orderDTOList = orderDTOPage.getRecords();

                //主订单id
                List<Long> orderIds = new ArrayList<>();
                for (OrderDTO orderDTO : orderDTOList) {
                    System.out.println(JSON.toJSONString(orderDTO));

                    //间隔天数
                    int receiptInterval = orderDTO.getList().get(0).getReceiptInterval();

                    if (receiptInterval == 0) {

                        //已过提货天数
                        int day = DateUtil.differentDaysByMillisecond(format.parse(LocalDateTime.parse(orderDTO.getDeliveryTime()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))), new Date());

                        if (day > 3) {
                            orderIds.add(orderDTO.getOrderId());
                        }

                    } else {

                        //已下单天数
                        int day = DateUtil.differentDaysByMillisecond(format.parse(orderDTO.getCreateTime()), new Date());
                        if (day >= (receiptInterval + 3)) {
                            orderIds.add(orderDTO.getOrderId());
                        }

                    }
                }
                if (orderIds.size() > 0) {
                    //批量核销
                    OrderMainIdsUpdateParam orderIdsParam = OrderMainIdsUpdateParam.builder().orderIds(orderIds).build();
                    orderFeignService.verificationBatchOrder(orderIdsParam);
                }

            }

            log.info("自动核销订单任务end----");

        } catch (Exception e) {

            log.error("订单核销任务异常：", e);
        }

    }
}
